package com.sservice.web.controller.admin;

import com.sservice.banfu.e.Device;
import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.service.DeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sservice.banfu.vo.DeviceVo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.utils.service.SService;
import com.sservice.common.Constants;
import com.sservice.urp.entity.User;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller
@RequestMapping("/admin/device/")
public class DeviceAdminController {

    @Autowired
    DeviceService deviceService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(Device device, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        device.setCreatorId(String.valueOf(user.getId()));
        return new BaseResult(true, String.valueOf(deviceService.save(device)), 200);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(Device device) {
        return new BaseResult(true, String.valueOf(deviceService.deleteById(device.getId())), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(Device device) {
        return new BaseResult(true, String.valueOf(deviceService.update(device)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(Device device) {
        Device device1 = deviceService.findById(device.getId());
        return new BaseResult(true, device1);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(DeviceVo device) {
        return deviceService.page(device);
    }
}

